// 首先引入Mock
import Mock from "mockjs";
import {initListVueVirtualUUID, isNullOrUndefined, strIsEmpty} from "./core";

/**
 * 模拟页面列表对象
 */
export const sksMockPageMixin={
    data(){
        return {
            mockPage: {
                list: [],
                total: 0,
            }
        }
    },
    created() {
        console.log("sksMockPage.created");
        this.initMockPage();
    },
    methods: {
        /**
         * 初始化模拟数据
         */
        initMockPage(){
            //制造测试数据
            let mock = Mock.mock(this.initMockParam());
            console.log("mockPage",mock)
            try{
                initListVueVirtualUUID(mock.list)
            }catch(e){
                console.warn("初始化模拟数据mock.list出错")
                console.warn(e)
            }
            this.mockPage=mock
        },
        /**
         * 初始化 mockPage 列表
         * @return {}  例如: {
                `list|100`:[{
                    personId: '@id',
                    description: '@cname',
                    'age|20-30': 23,
                    'job|1': ['前端工程师', '后端工程师', 'UI工程师', '需求工程师'],
                    remark: '@ctitle(2,4)'
                }],
                total: 100,
            }
         */
        initMockParam(){
            return {
                'list|100':[{
                    personId: '@id',
                    description: '@cname',
                    'age|20-30': 23,
                    'job|1': ['前端工程师', '后端工程师', 'UI工程师', '需求工程师'],
                    title: '@ctitle(20,40)',
                    remark: '@cword(20,40)'

                }],
                total: 100,
            }
        },
        //列表分页
        mockPagingList({pageNum=1,pageSize=10}){
            let list=this.mockPage.list
            let tmpPageNum=pageNum-1;
            if(tmpPageNum<0){
                return []
            }
            if(pageSize<=0){
                return []
            }
            let start=tmpPageNum*pageSize
            let tmp=[]
            for (let i = start; i < list.length; i++) {
                if(i>(start+pageSize)){
                   break
                }
                tmp.push(list[i])
            }

            return {
                list: tmp,
                total: this.mockPage.total
            }
        },
    }
}

